package com.songlanyun.modules.recommendassist.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.exception.DonationExceptionEnum;
import com.songlanyun.modules.recommendassist.dao.RecommendAssistDao;
import com.songlanyun.modules.recommendassist.entity.RecommendAssistEntity;
import com.songlanyun.modules.recommendassist.model.vo.RecommendAssistVO;
import com.songlanyun.modules.recommendassist.service.RecommendAssistService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("recommendAssistService")
public class RecommendAssistServiceImpl extends ServiceImpl<RecommendAssistDao, RecommendAssistEntity> implements RecommendAssistService {

    @Resource
    private AccountService accountService;

    @Resource
    private UserInfoService userInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RecommendAssistEntity> page = this.page(
                new Query<RecommendAssistEntity>().getPage(params),
                builderQuery(params)
        );

        page.getRecords().forEach(r -> {
            //推荐人姓名和手机号
            r.setRecommendName(userInfoService.getNameById(r.getUserId()));
            r.setRecommendMobile(accountService.getMobileByUserId(r.getUserId()));
        });

        return new PageUtils(page);
    }

    @Override
    public void contact(RecommendAssistEntity recommendAssist) {
        RecommendAssistEntity entity = this.loadById(recommendAssist.getId(), true);
        entity.setStatus(1);
        entity.setFeedback(recommendAssist.getFeedback());
        entity.setContactTime(new Date());
        this.baseMapper.updateById(entity);
    }

    @Override
    public RecommendAssistEntity loadById(Long id, boolean tw) {
        RecommendAssistEntity entity = this.baseMapper.selectById(id);
        if (ObjectUtil.isNull(entity) && tw) {
            throw new RRException(DonationExceptionEnum.THE_RECOMMENDED_HELP_RECORD_DOES_NOT_EXIST);
        }
        return entity;
    }

    @Override
    public List<RecommendAssistVO> selectVosList(Map<String, Object> params) {
        return this.baseMapper.selectVosList(builderQuery(params));
    }

    private LambdaQueryWrapper<RecommendAssistEntity> builderQuery(Map<String, Object> params) {
        String realName = MapUtil.getStr(params, "real_name");
        String phone = MapUtil.getStr(params, "phone");
        String createTimeStart = MapUtil.getStr(params, "create_time_start");
        String createTimeEnd = MapUtil.getStr(params, "create_time_end");
        Integer status = MapUtil.getInt(params, "status");

        return new LambdaQueryWrapper<RecommendAssistEntity>()
                .like(StringUtils.isNotBlank(realName), RecommendAssistEntity::getRealName, realName)
                .like(StringUtils.isNotBlank(phone), RecommendAssistEntity::getPhone, phone)
                .ge(StringUtils.isNotBlank(createTimeStart), RecommendAssistEntity::getCreateTime, createTimeStart)
                .le(StringUtils.isNotBlank(createTimeEnd), RecommendAssistEntity::getCreateTime, createTimeEnd)
                .eq(ObjectUtil.isNotNull(status), RecommendAssistEntity::getStatus, status)
                .orderByDesc(RecommendAssistEntity::getCreateTime);
    }

}
